package com.christina.service.impl;

import com.christina.service.CacheService;
import com.christina.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.messages.Message;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 缓存服务实现类
 * 
 * @author Christina
 */
@Slf4j
@Service
public class CacheServiceImpl implements CacheService {

    private final RedisUtil redisUtil;
    
    // 缓存过期时间常量
    private static final long CHAT_HISTORY_EXPIRE_SECONDS = 3600; // 1小时
    private static final long USER_INFO_EXPIRE_SECONDS = 1800; // 30分钟

    public CacheServiceImpl(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Override
    public void cacheChatHistory(Long userId, List<Message> messages) {
        try {
            String key = RedisUtil.getChatHistoryCacheKey(userId);
            redisUtil.set(key, messages, CHAT_HISTORY_EXPIRE_SECONDS);
            log.debug("缓存聊天历史成功，用户ID: {}, 消息数量: {}", userId, messages.size());
        } catch (Exception e) {
            log.error("缓存聊天历史失败，用户ID: {}", userId, e);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Message> getCachedChatHistory(Long userId) {
        try {
            String key = RedisUtil.getChatHistoryCacheKey(userId);
            Object cached = redisUtil.get(key);
            if (cached instanceof List) {
                List<Message> messages = (List<Message>) cached;
                log.debug("获取缓存聊天历史成功，用户ID: {}, 消息数量: {}", userId, messages.size());
                return messages;
            }
        } catch (Exception e) {
            log.error("获取缓存聊天历史失败，用户ID: {}", userId, e);
        }
        return null;
    }

    @Override
    public void clearChatHistoryCache(Long userId) {
        try {
            String key = RedisUtil.getChatHistoryCacheKey(userId);
            redisUtil.del(key);
            log.debug("清除聊天历史缓存成功，用户ID: {}", userId);
        } catch (Exception e) {
            log.error("清除聊天历史缓存失败，用户ID: {}", userId, e);
        }
    }

    @Override
    public void cacheUserInfo(Long userId, Object userInfo) {
        try {
            String key = RedisUtil.getUserCacheKey(userId);
            redisUtil.set(key, userInfo, USER_INFO_EXPIRE_SECONDS);
            log.debug("缓存用户信息成功，用户ID: {}", userId);
        } catch (Exception e) {
            log.error("缓存用户信息失败，用户ID: {}", userId, e);
        }
    }

    @Override
    public Object getCachedUserInfo(Long userId) {
        try {
            String key = RedisUtil.getUserCacheKey(userId);
            Object userInfo = redisUtil.get(key);
            if (userInfo != null) {
                log.debug("获取缓存用户信息成功，用户ID: {}", userId);
                return userInfo;
            }
        } catch (Exception e) {
            log.error("获取缓存用户信息失败，用户ID: {}", userId, e);
        }
        return null;
    }

    @Override
    public void clearUserInfoCache(Long userId) {
        try {
            String key = RedisUtil.getUserCacheKey(userId);
            redisUtil.del(key);
            log.debug("清除用户信息缓存成功，用户ID: {}", userId);
        } catch (Exception e) {
            log.error("清除用户信息缓存失败，用户ID: {}", userId, e);
        }
    }
}